package com.itheima.controller;

import com.itheima.pojo.AddIntegral;
import com.itheima.pojo.LoginDto;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 28066
 * @version 1.0
 * @description: TODO
 * @date 2022/11/12 18:42
 */
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findByClassId")
    public Result allStudentByClassId(HttpSession session){
        LoginDto loginDto = (LoginDto) session.getAttribute("LoginDto");
        Integer classId = loginDto.getClassId();
        List<User> userList=userService.findAllStudentByClassId(classId);
            return new Result(0,"操作成功",userList);
    }

    @PostMapping("/addIntegral")
    public Result addIntegral(@RequestBody AddIntegral addIntegral){
        //找到该学生的原有积分
        Integer studentIntegral=userService.findStudentIntergral(addIntegral.getId());
        addIntegral.setIntegral(addIntegral.getIntegral()+studentIntegral);
        int i=userService.updateStudentIntegral(addIntegral);
        if(i>0){
            return new Result(0,"成功");
        }
        return new Result(0,"失败");
    }
}
